package tq.com.tqstudent.ui.activity.classes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lib.common.base.BaseActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.event.CreateCommentEvent;
import tq.com.tqstudent.net.ok.StringCallback;

/**
 * @Author rames
 * @date 2019/2/28 22:17
 */
public class CreateCommentActivity extends BaseActivity {
    @BindView(R.id.content_edit)
    EditText contentEdit;
    @BindView(R.id.star_iv1)
    ImageView starIV1;
    @BindView(R.id.star_iv2)
    ImageView starIV2;
    @BindView(R.id.star_iv3)
    ImageView starIV3;
    @BindView(R.id.star_iv4)
    ImageView starIV4;
    @BindView(R.id.star_iv5)
    ImageView starIV5;
    @BindView(R.id.index_tv)
    TextView indexTV;
    private int score = 5;
    private String classID = "";

    public static void show(Activity activity, String classID) {
        Intent intent = new Intent(activity, CreateCommentActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("classID", classID);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void getExtras(Bundle extras) {
        classID = extras.getString("classID");
    }

    @Override
    public int initLayout() {
        return R.layout.activity_create_comment;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("新建评论");
        contentEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                indexTV.setText(contentEdit.getText().length() + "");
            }
        });
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.commit_tv, R.id.star_layout1, R.id.star_layout2, R.id.star_layout3, R.id.star_layout4, R.id.star_layout5})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.commit_tv:
                if (contentEdit.getText().toString().length() < 1) {
                    showToast("请输入评价内容再提交!");
                    return;
                }

                commitTask();
                break;

            case R.id.star_layout1:
                setStarLevel(1);
                break;
            case R.id.star_layout2:
                setStarLevel(2);
                break;
            case R.id.star_layout3:
                setStarLevel(3);
                break;
            case R.id.star_layout4:
                setStarLevel(4);
                break;
            case R.id.star_layout5:
                setStarLevel(5);
                break;
        }
    }

    private void commitTask() {
        String content = contentEdit.getText().toString();
        String json = "{\"content\":\"" + content + "\",\"score\":" + score + "}";

        OkGo.<String>post(App.getHttpUrl("api/clz/comm"))
                .tag(this)
                .upJson(json)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject object = new JSONObject(response.body());
                            if (object.getInt("ret") == 0) {
                                showToast("评价成功");
                                EventBus.getDefault().post(new CreateCommentEvent());
                                CreateCommentActivity.this.finish();

                            } else {
                                showToast(object.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    private void setStarLevel(int index) {
        score = 5;
        starIV1.setImageResource(R.mipmap.icon_star_checked);
        starIV2.setImageResource(R.mipmap.icon_star_checked);
        starIV3.setImageResource(R.mipmap.icon_star_checked);
        starIV4.setImageResource(R.mipmap.icon_star_checked);
        starIV5.setImageResource(R.mipmap.icon_star_checked);

        if (index == 1) {
            score = 1;
            starIV2.setImageResource(R.mipmap.icon_star_normal);
            starIV3.setImageResource(R.mipmap.icon_star_normal);
            starIV4.setImageResource(R.mipmap.icon_star_normal);
            starIV5.setImageResource(R.mipmap.icon_star_normal);
        } else if (index == 2) {
            score = 2;
            starIV3.setImageResource(R.mipmap.icon_star_normal);
            starIV4.setImageResource(R.mipmap.icon_star_normal);
            starIV5.setImageResource(R.mipmap.icon_star_normal);
        } else if (index == 3) {
            score = 3;
            starIV4.setImageResource(R.mipmap.icon_star_normal);
            starIV5.setImageResource(R.mipmap.icon_star_normal);
        } else if (index == 4) {
            score = 4;
            starIV5.setImageResource(R.mipmap.icon_star_normal);
        }
    }
}
